package functions;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Scanner;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.SoftBevelBorder;

import views.DayView;

import calendar_ex.Appointment;
import calendar_ex.Appointment.Recurrence;
import calendar_ex.CalendarDate;
import calendar_ex.CalendarEx;
import calendar_ex.CalendarTime;



public class AddAppointment extends JPanel {

	/*
	 * 
	 */
	private static final long serialVersionUID = 1L;

	Appointment add_appointment;

	JTextArea name, month, year, start_time, end_time, recurs, location, category, description,
	date;
	
	JTextField intro;
	
	JButton addb;
	
	Recurrence r; 
	
	
	private void add_b(JTextArea jt, String s){
		jt.setText(s);
		jt.setBackground(Color.LIGHT_GRAY);
		jt.setBorder(new SoftBevelBorder(BevelBorder.RAISED));
		jt.setFont(new Font("Arial", Font.BOLD, 18));
		
	}
	
	private void small_add_b(JTextArea jt, String s){
		jt.setText(s);
		jt.setBackground(Color.LIGHT_GRAY);
		jt.setBorder(new SoftBevelBorder(BevelBorder.RAISED));
		jt.setFont(new Font("Arial", Font.BOLD, 13));
		
	}
	
	
	public AddAppointment(){
		
		JTable add_ap = new JTable();
		JPanel buttons = new JPanel();
		JPanel date_input = new JPanel();
		
		
		add_ap.setLayout(new BoxLayout(add_ap, BoxLayout.X_AXIS));
		buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
		date_input.setLayout(new BoxLayout(date_input, BoxLayout.X_AXIS));
		
		add_ap.add(buttons, BorderLayout.SOUTH);
		add_ap.add(date_input, BorderLayout.CENTER);
		         
		intro = new JTextField(15);
		intro.setEditable(true);
		intro.setText("Add Appointment");
		intro.setFont(new Font("Courier New", Font.BOLD, 24));
		
	    
	    addb = new JButton();
	    addb.setText("Save");
	    addb.setFont(new Font("Arial", Font.BOLD, 18));
	    addb.setBackground(Color.RED);
	   
		
		name = new JTextArea(4, 24);
		add_b(name, "Event Name");
		
		date = new JTextArea(2, 5);
		small_add_b(date, "Start Date eg 01/02/1992");
		
		start_time = new JTextArea(2, 6);
		small_add_b(start_time, "Start Time eg 09:30");
		
		end_time = new JTextArea(2, 6);
		small_add_b(end_time, "End Time eg 15:30");
		
	
		
		description = new JTextArea(4, 24);
		add_b(description, "Description");
		
		location = new JTextArea(4, 24);
		add_b(location, "Location");
		
		
	
		add(intro);
		add(name);
		add(description);
		add(location);
		date_input.add(date);
		date_input.add(start_time);
		date_input.add(end_time);
		//buttons.add(addb, BorderLayout.CENTER);
		
		//add(category);
		add(date_input, BorderLayout.CENTER);
		add(buttons);
		add(add_ap);
		
		
		String[] choices1 = {"DAILY", "WEEKLY", "FORTNIGHTLY", "MONTHLY"};
		String[] choices2 = {"Work", "Uni", "Social"};

		final JComboBox recurring;
		final JComboBox category;
		
		recurring = new JComboBox(choices1);
		category = new JComboBox(choices2);
		
		
		addb.addActionListener(new ActionListener()
		{
			  public void actionPerformed(ActionEvent e)
			  {
	          String recur = (String) recurring.getSelectedItem();
			  String cate = (String) category.getSelectedItem();
			  Scanner datescan = new Scanner(date.getText());
			  datescan.useDelimiter("/");
			 
			  int day = Integer.parseInt(datescan.next());
			  int month = Integer.parseInt(datescan.next());
			  int year = Integer.parseInt(datescan.next());
			  
			  
			  Scanner starttimescan = new Scanner(start_time.getText());
			  starttimescan.useDelimiter(":");
			  
			  int shour = Integer.parseInt(starttimescan.next());
			  int smins = Integer.parseInt(starttimescan.next());
	
			  
			  Scanner endtimescan = new Scanner(end_time.getText());
			  endtimescan.useDelimiter(":");
			  
			  int ehour = Integer.parseInt(endtimescan.next());
			  int emins = Integer.parseInt(endtimescan.next());
			  
			 
			  
			  CalendarDate date = new CalendarDate(day, month, year);
			  CalendarTime stime = new CalendarTime(shour, smins);
			  CalendarTime etime = new CalendarTime(ehour, emins);
					  
			 
			  add_appointment = new Appointment(date, stime, etime, description.getText(), location.getText(), cate);
			  CalendarEx newData = new CalendarEx();
			  newData.openCalendar("calendar.txt");
			  newData.addAppointment(add_appointment);
			  newData.saveCalendar("calendar.txt");

			  
			  } 
			});

		JPanel content = new JPanel(new GridLayout(2,3,5,5));
		content.add(new Label("Recurring"));
		content.add(new Label("Category"));
		content.add(new Label(""));
		content.add(recurring);
		content.add(category);
		content.add(addb);	
		add(content);
	}

}